package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hoperun.bean.AppUserFriends;
import com.hoperun.dao.AppUserFriendsDAO;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.service.IAppUserFriendsSV;
import com.hoperun.utils.Constant;


@Service("appUserFriendsSV")
public class AppUserFriendsSVImpl implements IAppUserFriendsSV
{     
	@Resource
	private AppUserFriendsDAO appUserFriendsDAO;

	@Override
	public void delete(long id) throws Exception
	{
		appUserFriendsDAO.delete(id);
	}

	@Override
	public AppUserFriends queryById(long id) throws Exception
	{
		return appUserFriendsDAO.queryById(id);
	}

	@Override
	public AppUserFriends save(AppUserFriends appUserFriends) throws Exception
	{
		appUserFriendsDAO.insert(appUserFriends);
		return appUserFriends;
	}

	@Override
	public void update(AppUserFriends appUserFriends) throws Exception
	{
		appUserFriendsDAO.update(appUserFriends);
	}

	@Override
	public void saveOrUpdate(AppUserFriends appUserFriends)throws Exception
	{
		//TODO
	}

	@Override
	public void saveOrUpdate(List<AppUserFriends> list)throws Exception
	{
		if(list != null && list.size() > 0)
		{
			for(AppUserFriends appUserFriends : list)
			{
				saveOrUpdate(appUserFriends);
			}
		}
	}

	@Override
	public List<AppUserFriends> queryByCondition(Criteria criteria)throws Exception
	{
		return appUserFriendsDAO.queryByCondition(criteria);
	}
	
	@Override
	public int queryCountByCondition(Criteria criteria)throws Exception
	{
		return appUserFriendsDAO.queryCountByCondition(criteria);
	}
	
	@Override
	public BootGridPage<AppUserFriends> queryPage(Criteria criteria,
			BootGrid<AppUserFriends> grid) throws Exception 
	{

//		AppUserFriends model = grid.getEntity();

		int totalCount = appUserFriendsDAO.queryCountByCondition(criteria);

		if (0 < totalCount) {
			
			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (null != sorts && sorts.size() > Constant.ZERO) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if(grid.getCurrent() >= Constant.ZERO){
				int start = (grid.getCurrent() - Constant.ONE) * grid.getRowCount();
				int size = grid.getRowCount();
	
				criteria.limit(start, size);
			}
			List<AppUserFriends> list = appUserFriendsDAO
					.queryByCondition(criteria);
			BootGridPage<AppUserFriends> page = new BootGridPage<AppUserFriends>(grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
        return new BootGridPage<AppUserFriends>(grid.getCurrent(), grid.getRowCount(), totalCount, new ArrayList<AppUserFriends>());

	}
}
